import { ChangeDetectorRef, Component, OnInit } from '@angular/core';
import { CourseLoaderService } from '../course/course-loader.service';
import Dexie from 'dexie';
import { DBService } from '../db/db.service';
import { from } from 'rxjs';
import { Router } from '@angular/router';
import { ExtendedCourse } from '../course/Course';
import { Title } from '@angular/platform-browser';

@Component({ selector: 'app-home', templateUrl: './home.component.html', styleUrls: ['./home.component.scss'] })
export class HomeComponent implements OnInit {
    courses: ExtendedCourse[] = [];
    constructor(private courseLoader: CourseLoaderService, private dbService: DBService, private router: Router,  private cdr : ChangeDetectorRef, private title: Title) { }
    name = '勇者';
    info: any;
    loading = true;
    ngOnInit() {
        this.title.setTitle('关于我在出现各种离谱BUG的平台上艰难学习Python基础语法这件事');
        this.info = this.dbService.table('info');
        from(this.info.where("key").equals("name").first()).subscribe(name => {
            if (name) {
                this.name = (name as any)['value'] as string;
            }
        })
        from(this.courseLoader.courseList()).subscribe((courseList)=>{
            from(this.courseLoader.extend(courseList)).subscribe(extended => {
                this.courses = extended;
                // this.cdr.detectChanges();
            });
        })
    }

    navigate(commands: string[]) {
        this.router.navigate(commands);
    }

    setName() {
        this.name = prompt('应该如何称呼你呢？') ?? '勇者';
        this.info.put({ key: "name", value: this.name });
    }
    
}
